/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.war.room;

import it.mdc.hearts.war.room.core.GameTable;
import it.mdc.hearts.war.room.core.UserInfo;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "sessionUserBean")
@SessionScoped
public class SessionUserBean implements Serializable{
    
    @ManagedProperty(value = "#{mainRoomBean}") // MY SESSION BEAN
    private MainRoomBean room;
    
    private UserInfo userInfo;
    private Integer ID;
    
    public void setRoom(MainRoomBean room) {
        this.room = room;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public Integer getID() {
        return ID;
    }

    public void setID(Integer ID) {
        this.ID = ID;
    }
    
    public void InitUser(UserInfo userInfo) {
        this.userInfo = userInfo;
        room.userJoinRoom(ID, userInfo);
    }
    
    public void removeUser() {
        if(isJoinedInTable())
        {
            if(userInfo.getCurrentTable().isPlayer(userInfo.getUserID()))
            {
                if(userInfo.getCurrentTable().getOwnerID() == userInfo.getUserID())
                {
                    room.removeGameTable(userInfo.getCurrentTable().getTableKey());
                }
                userInfo.getCurrentTable().removePlayer(userInfo);
            }
            else
            {
                userInfo.getCurrentTable().removeViewer(userInfo);
            }
        }
        room.userRemoveRoom(ID);
    }

    boolean isJoinedInTable() {
        if(this.userInfo.getCurrentTable() != null)
        {
            return true;
        }
        return false;
    }
}
